//
//  IGBatchRequest.m
//  jingdongfang
//
//  Created by BOE on 2018/5/2.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGBatchRequest.h"

@implementation IGBatchRequest

- (void)startWithBaseRequest:(NSArray<IGBaseRequest *> *)baseRequestArr decodeClassArr:(NSArray<Class> *)decodeClassArr CompletionBlockWithSuccess:(IGBatchRequestCompletionBlock)completion {
    IGBatchRequest *batchRequest = [self initWithRequestArray:baseRequestArr];
    
    NSMutableArray *responseObjectArr = [NSMutableArray array];
    
    [batchRequest startWithCompletionBlockWithSuccess:^(YTKBatchRequest * _Nonnull batchRequest) {
        NSArray *requests = batchRequest.requestArray;
        __block BOOL flag = false;
        for (int i = 0; i < requests.count; i++) {
            IGBaseRequest *request = requests[i];
            request.decodeClass = decodeClassArr[i];
            [request parseResponseObject:request.responseObject error:nil completeBlock:^(id responseObject, NSError *error) {
                if (responseObject) {
                    [responseObjectArr addObject:responseObject];
                } else {
                    flag = true;
                }
            }];
            if (flag) {
                completion(nil, nil, false);
                return;
            }
        }
        
        completion(responseObjectArr, nil, true);
    } failure:^(YTKBatchRequest * _Nonnull batchRequest) {
        completion(nil, nil, false);
    }];
}

@end
